//
//  MVPListViewController.m
//  iOSCodeLearning
//
//  Created by TFAppleWork-Summer on 2018/1/12.
//  Copyright © 2018年 Ace. All rights reserved.
//

#import "MVPListViewController.h"
#import "GoodSectionPresenter.h"
#import "BookSectionPresenter.h"

#import "CrowdFundingBookListModel.h"
#import "GroupBuyingBookModel.h"
#import "CrowdFundingBookModel.h"
#import "CrowdBooksBookModel.h"

@interface MVPListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *sectionPArray;

@end

@implementation MVPListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.sectionPArray = [NSMutableArray arrayWithCapacity:0];
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
    [self.collectionView registerClass:[GoodTableViewCell class] forCellWithReuseIdentifier:NSStringFromClass([GoodSectionPresenter class])];
    [self.collectionView registerClass:[BooksCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([BookSectionPresenter class])];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];

    
    [self p_requestListData];
    
}
- (void)p_requestListData{
    [[NetWorkAssistant shareNetWorkAssistant] setRequestHeaderWithDict:@{@"TOKEN" :@"Timeface-iOS",
                                                                         @"USERID" :@"718385313074",
                                                                         @"DEVICEID" :[[[UIDevice currentDevice] identifierForVendor] UUIDString].mutableCopy
                                                                         }];

    [[NetWorkAssistant shareNetWorkAssistant] get:@"http://stg2.v5time.net/tfmobile/crowdfunding/index"
                                       parameters:@{@"isGzip" : @"0"}
                                         progress:nil
                                          success:^(id resposeObject) {
                                              NSLog(@"response :%@",resposeObject);
                                              
                                             CrowdFundingBookListModel *crowdListModel =  [[CrowdFundingBookListModel alloc] initWithDictionary:resposeObject error:nil];
                                              
                        
                                              
                                              if (crowdListModel.groupBuying != nil && crowdListModel.groupBuying.count > 0) {
                                                  GoodSectionPresenter *sp = [[GoodSectionPresenter alloc] init];
                                                  sp.dataArray = crowdListModel.groupBuying;
                                                  [self.sectionPArray addObject:sp];
                                              }
                                              
                                              // books
                                              NSArray *booksArray = [resposeObject objectForKey:@"books"];
                                              NSMutableArray *booksCrowdArray = [NSMutableArray arrayWithCapacity:0];
                                              
                                              for (NSDictionary *dic in booksArray) {
                                                   NSError *modelError;
                                                  CrowdModel *model = [[CrowdModel alloc] initWithDictionary:dic error:nil];
                                                  [booksCrowdArray addObject:model];
                                              }
                                              
                                              if (booksArray !=nil && booksArray.count>0) {
                                                  BookSectionPresenter *sp = [[BookSectionPresenter alloc] init];
                                                  sp.dataArray = booksCrowdArray;
                                                  [self.sectionPArray addObject:sp];
                                              }
                                              
                                              [self.collectionView reloadData];
                                          }
                                          failure:^(NSError * _Nonnull error) {
                                              NSLog(@"error : %@",error);
                                          }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.sectionPArray.count;
//    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        id<SectionPresenterProtocol>sp = [self.sectionPArray objectAtIndex:section];
        return [sp itemCount];
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    id<SectionPresenterProtocol>sp = [self.sectionPArray objectAtIndex:indexPath.section];
    id cell =  [sp collectionView:collectionView forIndexPath:indexPath];
    return cell;

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    id <SectionPresenterProtocol> sp = [self.sectionPArray objectAtIndex:indexPath.section];
    return [sp headerViewWithCollectionView:collectionView indexpath:indexPath];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        id <SectionPresenterProtocol> sp = [self.sectionPArray objectAtIndex:indexPath.section];
        return [sp headerViewWithCollectionView:collectionView indexpath:indexPath];
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(self.view.frame.size.width, 44);
}

- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath {

    id <SectionPresenterProtocol> sp = [self.sectionPArray objectAtIndex:indexPath.section];
    return [sp sizeForItemAtIndex:indexPath.row];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 这个方法会让当前选中的cell滚到最中间
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];
}



#pragma mark - lazy-load
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *itemLayout = [[UICollectionViewFlowLayout alloc]init];
        itemLayout.itemSize = CGSizeMake(100 , 100);
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:itemLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
